package com.ouc.yyi.user.springcloud.user.wx.aop;

import com.ouc.yyi.user.springcloud.user.wx.annocation.BindOperKey;
import com.ouc.yyi.user.springcloud.user.wx.annocation.Primary;
import com.ouc.yyi.user.springcloud.user.wx.bo.RelPO;
import com.ouc.yyi.user.springcloud.user.wx.bo.WxInfo;
import com.ouc.yyi.user.springcloud.user.wx.dao.GetNewCustomerMapper;
import com.ouc.yyi.user.springcloud.user.wx.enums.UserType;
import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;


/**
 * @description:
 * @author: yyi
 * @date: 2022/8/26 10:59
 * @version: 1.0
 **/

@Aspect
@Component
public class AopBindOperKey {

    @Pointcut("@annotation(com.ouc.yyi.user.springcloud.user.wx.annocation.BindOperKey))")
    public void cut(){
        //空方法
    }
    @Around("cut() && @annotation(bindOperKey)")
    @SneakyThrows
    public Object doLimit(ProceedingJoinPoint point, BindOperKey bindOperKey) {
        //获取注解中的属性
        UserType value = bindOperKey.value();
        if (value == UserType.MANAGER) {
            //后置通知处理
            WxInfo relPO = (WxInfo) point.getArgs()[0];
            //并对标记为Primary注解的字段添加前缀ceb
            Class<?> clazz = point.getArgs()[0].getClass();
            if (clazz != Object.class) {
                for (Field field : clazz.getDeclaredFields()) {
                    //获取当前字段上是否存在注解
                    Primary annotation = field.getAnnotation(Primary.class);
                    if (annotation != null) {
                        field.setAccessible(true);
                        System.err.println(field.getName());
                        field.set(point.getArgs()[0], "cebbank" + field.get(relPO));
                    }
                }
            }
            return point.proceed();
        }
        return point.proceed();
    }

}
